package com.vchello.service.impl.entrepren;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vchello.bean.entrepren.ProDelivery;
import com.vchello.bean.entrepren.ProDeliveryMilepost;
import com.vchello.bean.entrepren.ProDeliveryOrganization;
import com.vchello.bean.file.Folder;
import com.vchello.bean.pro.Pro;
import com.vchello.bean.pro.ProLables;
import com.vchello.bean.pro.ProMilepost;
import com.vchello.bean.pro.ProOrganization;
import com.vchello.dao.entrepren.ProDeliveryDao;
import com.vchello.dao.entrepren.ProDeliveryMilepostDao;
import com.vchello.dao.entrepren.ProDeliveryOrganizationDao;
import com.vchello.dao.file.FolderDao;
import com.vchello.dao.project.ProLabelDao;
import com.vchello.dao.project.ProMilepostDao;
import com.vchello.dao.project.ProOrganizationDao;
import com.vchello.dao.project.ProjectDao;
import com.vchello.service.entrepren.ProDeliveryService;
import com.vchello.util.ConvertBeanUtil;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Service(value = "proDeliveryService")
public class ProDeliveryServiceImpl extends ServiceImpl<ProDeliveryDao, ProDelivery> implements ProDeliveryService {
	
	private static final int TYPE = 1;   //项目类型 1主动创建 2:收到          
	private static final int HIDE = 1;   //是否隐藏投递创建的项目资料如果没有完善，设置为隐藏，对所有接收到项目的机构、个人都隐藏  1:否 2:是
	@Autowired
	private ProDeliveryDao proDeliveryDao;
	@Autowired
	private ProDeliveryMilepostDao proDeliveryMilepostDao;
	@Autowired
	private ProDeliveryOrganizationDao proDeliveryOrganizationDao;
	@Autowired
	private ProjectDao projectDao;
	@Autowired
	private ProLabelDao proLabelDao;
	@Autowired
	private ProMilepostDao proMilepostDao;
	@Autowired
	private ProOrganizationDao proOrganizationDao;
	@Autowired
	private FolderDao folderDao;
	
	@Override
	public Page<ProDelivery> findProDeliveryByPage(Page<ProDelivery> page, ProDelivery proDelivery) {
		Wrapper<ProDelivery> wrapper = new EntityWrapper<ProDelivery>(proDelivery);
		return selectPage(page, wrapper);
	}
	
	@Override
	public Integer saveProDelivery(ProDelivery entity) {
		return proDeliveryDao.insert(entity);
	}

	@Override
	public Integer updateProDelivery(ProDelivery entity) {
		return proDeliveryDao.updateById(entity);
	}

	@Override
	public ProDelivery findProDeliveryById(int id) {
		return proDeliveryDao.selectById(id);
	}

	@Override
	public ProDelivery findProDeliveryByUserId(int userId) {
		return proDeliveryDao.findProDeliveryByUserId(userId);
	}

	@Override
	public boolean createDeliveryProject(ProDelivery entity, List<ProDeliveryOrganization> proDeliveryOrganizations,
			List<ProDeliveryMilepost> proDeliveryMileposts, int userId, int deliveryId) {
		boolean isTure = (entity.getId() == 0) ? proDeliveryDao.insert(entity)>0 : proDeliveryDao.updateById(entity)>0;
		if(!isTure) {
			return false;
		}
		try {
			//创业者项目里程碑
			for (ProDeliveryMilepost milepost : proDeliveryMileposts) {
				if(milepost.getId() == 0) {
					milepost.setProId(entity.getId());
				}
				proDeliveryMilepostDao.updateById(milepost);
			}
			//创业者项目团队
			for (ProDeliveryOrganization organization : proDeliveryOrganizations) {
				if(organization.getId() == 0) {
					organization.setProId(entity.getId());
				} 
				proDeliveryOrganizationDao.updateById(organization);
			}
			
			if(isTure) {   //投递入库投资者或者机构的项目表
				Pro pro = copyPro(entity, deliveryId);
				projectDao.insert(pro);
				for(ProLables label : createProLabel(entity, pro)){
					proLabelDao.insert(label);
				}
				for(ProMilepost mile : createProMilepost(pro, proDeliveryMileposts)){
					proMilepostDao.insert(mile);
				}
				for(ProOrganization organiza : createProOrganization(pro, proDeliveryOrganizations)){
					proOrganizationDao.insert(organiza);
				}
				Folder folder = folderDao.findProjectFolder(pro.getUserId());
				if(null != folder) {
					folderDao.insert(new Folder(folder.getName(), folder.getUserId(), pro.getId(), folder.getId(), Utility.getSimpleFormatedDayNow()));
				}
				isTure = true;
			}
		} catch (Exception e) {
			isTure = false;
		}
		
		return isTure;
		
		
	}
	
	private Pro copyPro(ProDelivery entity, int deliveryId){
		Pro newPro = new Pro();
		newPro.setAreaId(entity.getAreaId());
		newPro.setFieldId(entity.getFieldsId());
		newPro.setRoundId(entity.getRoundId());
		Pro pro = (Pro) ConvertBeanUtil.coverBeanToBean(entity, newPro);
		pro.setId(0);
		pro.setType(TYPE);
		pro.setHide(HIDE);
		pro.setUserId(deliveryId);
		pro.setCurrency(entity.getUnit());
		return pro;
	}
	
	private List<ProLables> createProLabel(ProDelivery entity, Pro pro){
		String[] lables = entity.getLables().split(";");
		List<ProLables> proLablesList = new ArrayList<ProLables>();
		ProLables proLables = null;
		for (int i = 0; i < lables.length; i++) {
			proLables = new ProLables();
			proLables.setProId(pro.getId());
			proLables.setName(lables[i]);
			proLablesList.add(proLables);
		}
		return proLablesList;
	}
	
	private List<ProMilepost> createProMilepost(Pro pro, List<ProDeliveryMilepost> proDeliveryMileposts){
		List<ProMilepost> saveProMilepostList = new ArrayList<ProMilepost>();
		ProMilepost proMilepost = null;
		for (ProDeliveryMilepost proDeliveryMilepost : proDeliveryMileposts) {
			proMilepost = new ProMilepost();
			proMilepost.setProId(pro.getId());
			proMilepost.setDetail(proDeliveryMilepost.getDetail());
			proMilepost.setTime(proDeliveryMilepost.getCreateTime());
			saveProMilepostList.add(proMilepost);
		}
		return saveProMilepostList;
		
	}
	private List<ProOrganization> createProOrganization(Pro pro, List<ProDeliveryOrganization> proOrganizations){
		List<ProOrganization> saveProOrganizationList = new ArrayList<ProOrganization>();
		ProOrganization proOrganization = null;
		for (ProDeliveryOrganization proDeliveryOrganization : proOrganizations) {
			proOrganization = new ProOrganization();
			proOrganization.setProId(pro.getId());
			proOrganization.setDetail(proDeliveryOrganization.getDetail());
			proOrganization.setName(proDeliveryOrganization.getName());
			proOrganization.setPhone(proDeliveryOrganization.getPhone());
			proOrganization.setPosition(proDeliveryOrganization.getPosition());
			saveProOrganizationList.add(proOrganization);
		}
		return saveProOrganizationList;
		
	}
}
